package com.hongkuncheng.cms.controller.admin;

import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hongkuncheng.cms.helper.*;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.model.Admin;
import com.hongkuncheng.cms.model.Article;
import com.hongkuncheng.cms.model.ArticleState;
import com.hongkuncheng.cms.model.Gather;
import com.hongkuncheng.cms.model.Organization;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 数据采集
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/articlegather")
public class AarticlegatherController extends BaseController {

    // 采集项目
    @RequestMapping
    public String index() {
        List<Gather> gathers = factorySession.createCriteria(Gather.class).list();
        request.setAttribute("gathers", gathers);
        return view();
    }

    // 开始采集
    @RequestMapping("gather")
    public String gather() {
        checkAdminPermission("office_article_gather");
        int gatherId = Integer.parseInt(request.getParameter("gatherid"));
        boolean namefilter = Boolean.parseBoolean(request.getParameter("namefilter"));
        Gather gather = factorySession.get(Gather.class, gatherId);
        request.setAttribute("gather", gather);
        Pattern patternList = Pattern.compile(gather.getRegexlist());
        Pattern patternLinkurl = Pattern.compile(gather.getRegexlinkurl());
        try {
            String htmlStr = HttpHelper.get(gather.getTargeturl(), gather.getEncode()).replaceAll(
                    "\r|\n|\t", "");
            if (!htmlStr.isEmpty()) {
                Matcher matcherList = patternList.matcher(htmlStr);
                // 临时采集集合
                List<Map<String, Object>> articles = new ArrayList<Map<String, Object>>();
                while (matcherList.find()) {
                    String list = matcherList.group("list");
                    Matcher matcherLinkurl = patternLinkurl.matcher(list);
                    while (matcherLinkurl.find()) {
                        String title = matcherLinkurl.group("title");
                        if (namefilter) {
                            int count = Integer.parseInt(factorySession.createCriteria(Article.class)
                                    .add(Restrictions.eq("title", title))
                                    .setProjection(Projections.countDistinct("id"))
                                    .uniqueResult().toString());
                            if (count > 0) continue;
                        }
                        String url = matcherLinkurl.group("url");
                        if (!url.startsWith("http")) url = gather.getTargeturl() + url;
                        Map<String, Object> article = new HashMap<String, Object>();
                        article.put("title", title);
                        article.put("url", url);
                        articles.add(article);
                    }
                }
                request.setAttribute("articles", articles);
            }
            return view();
        } catch (Exception e) {
            PageHelper.alertAndRedirect(e.getMessage(), "./");
            return null;
        }
    }

    // 接收并导入草稿箱
    @RequestMapping("passimport")
    public String passimport() {
        checkAdminPermission("office_article_gather");
        String pageurls = request.getParameter("pageurls");
        request.setAttribute("pageurls", pageurls);
        request.setAttribute("datacount", pageurls.split(",").length);
        int gatherId = Integer.parseInt(request.getParameter("gatherid"));
        Gather gather = factorySession.get(Gather.class, gatherId);
        request.setAttribute("regexdetail", SecurityHelper.aesEncrypt(gather.getRegexdetail()));
        request.setAttribute("encode", gather.getEncode());
        request.setAttribute("outlink", gather.isOutlink());
        Organization organization = (Organization) factorySession.createCriteria(Organization.class)
                .add(Restrictions.eq("project", applicationName))
                .setMaxResults(1).uniqueResult();
        request.setAttribute("source", organization.getName());
        return view();
    }

    // 接收并导入草稿箱（供Ajax调用）
    @RequestMapping("passimportdo")
    @ResponseBody
    public void passimportdo() {
        ArticleState articleState = factorySession.load(ArticleState.class, 1);
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        String pageurl = request.getParameter("pageurl");
        String regexdetail = SecurityHelper.aesDecrypt(request.getParameter("regexdetail"));
        String encode = request.getParameter("encode");
        boolean outlink = Boolean.getBoolean(request.getParameter("outlink"));
        String source = request.getParameter("source");
        Pattern patternDetail = Pattern.compile(regexdetail);
        try {
            String htmlStr = HttpHelper.get(pageurl, encode).replaceAll("\r|\n|\t", "");
            if (!htmlStr.isEmpty()) {
                URL url = new URL(pageurl);
                String host = url.getHost();
                String hostFull = pageurl.substring(0, pageurl.indexOf(host) + host.length());
                Matcher matcherDetail = patternDetail.matcher(htmlStr);
                while (matcherDetail.find()) {
                    Article article = new Article();
                    article.setArticleState(articleState);
                    article.setAdmin(admin);
                    article.setTitle(matcherDetail.group("title"));
                    article.setBold(false);
                    article.setRed(false);
                    article.setSource(regexdetail.contains("(?<source>.*?)") ? matcherDetail.group("source") : source);
                    article.setAuthor(regexdetail.contains("(?<author>.*?)") ? matcherDetail.group("author") :
                            admin.getName());
                    if (outlink) {
                        article.setExturl(pageurl);
                    } else {
                        String contents = StringHelper.escapeXss(matcherDetail.group("contents"));
                        if (!contents.isEmpty()) {
                            Pattern patternImage = Pattern.compile("src\\s*=\\s*[\"'](?<src>.*?)[\"'](.*?)",
                                    Pattern.CASE_INSENSITIVE);
                            Matcher matcherImage = patternImage.matcher(contents);
                            Set<String> imgurls = new HashSet<>();
                            while (matcherImage.find()) {
                                String imgurl = matcherImage.group("src");
                                if (!imgurl.startsWith("http") && !imgurls.contains(imgurl)) {
                                    String imgurlFull = imgurl.startsWith("/") ? (hostFull + imgurl) : (pageurl.substring(0, pageurl.lastIndexOf("/") + 1) + imgurl);
                                    contents = contents.replace(imgurl, imgurlFull);
                                    imgurls.add(imgurl);
                                }
                            }
                            article.setContents(contents);
                            article.setContenttext(StringHelper.escapeHtml(contents));
                        }
                    }
                    article.setPublishtime(new Date());
                    article.setClicknum(0);
                    article.setSort(0);
                    factorySession.save(article);
                }
            }
        } catch (Exception e) {
            print("URL解析错误<br />" + e.getMessage());
        }
    }

    // 自动采集
//    @Scheduled(cron = "0 0 * * * ?")
    @RequestMapping("autoGather")
    @ResponseBody
    public void autoGather() {
        System.out.println("自动采集...");
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        ArticleState articleState = factorySession.load(ArticleState.class, 4);
        List<Gather> gathers = factorySession.createCriteria(Gather.class)
                .add(Restrictions.eq("autogather", true)).list();
        for (Gather gather : gathers) {
            Pattern patternList = Pattern.compile(gather.getRegexlist());
            Pattern patternLinkurl = Pattern.compile(gather.getRegexlinkurl());
            try {
                String htmlStr = HttpHelper.get(gather.getTargeturl(), gather.getEncode()).replaceAll(
                        "\r|\n|\t", "");
                if (!htmlStr.isEmpty()) {
                    Matcher matcherList = patternList.matcher(htmlStr);
                    while (matcherList.find()) {
                        String list = matcherList.group("list");
                        Matcher matcherLinkurl = patternLinkurl.matcher(list);
                        while (matcherLinkurl.find()) {
                            String title = matcherLinkurl.group("title");
                            System.out.println("title:"+title);
                            int count = Integer.parseInt(factorySession.createCriteria(Article.class)
                                    .add(Restrictions.eq("title", title))
                                    .setProjection(Projections.countDistinct("id"))
                                    .uniqueResult().toString());
                            if (count > 0) continue;
                            String url = matcherLinkurl.group("url");
                            if (!url.startsWith("http")) url = gather.getTargeturl() + url;
                            System.out.println("url:"+url);
                            String source = request.getParameter("source");
                            Pattern patternDetail = Pattern.compile(gather.getRegexdetail());
                            String htmlStrDetal = HttpHelper.get(url, gather.getEncode()).replaceAll("\r|\n|\t", "");
                            if (!htmlStrDetal.isEmpty()) {
                                URL urlObj = new URL(url);
                                String host = urlObj.getHost();
                                String hostFull = url.substring(0, url.indexOf(host) + host.length());
                                Matcher matcherDetail = patternDetail.matcher(htmlStrDetal);
                                while (matcherDetail.find()) {
                                    Article article = new Article();
                                    article.setArticleState(articleState);
                                    article.setAdmin(admin);
                                    article.setTitle(matcherDetail.group("title"));
                                    article.setBold(false);
                                    article.setRed(false);
                                    article.setSource(gather.getRegexdetail().contains("(?<source>.*?)") ? matcherDetail.group("source") : source);
                                    article.setAuthor(gather.getRegexdetail().contains("(?<author>.*?)") ? matcherDetail.group("author") :
                                            admin.getName());
                                    if (gather.isOutlink()) {
                                        article.setExturl(url);
                                    } else {
                                        String contents = StringHelper.escapeXss(matcherDetail.group("contents"));
                                        if (!contents.isEmpty()) {
                                            Pattern patternImage = Pattern.compile("src\\s*=\\s*[\"'](?<src>.*?)[\"'](.*?)",
                                                    Pattern.CASE_INSENSITIVE);
                                            Matcher matcherImage = patternImage.matcher(contents);
                                            Set<String> imgurls = new HashSet<>();
                                            while (matcherImage.find()) {
                                                String imgurl = matcherImage.group("src");
                                                if (!imgurl.startsWith("http") && !imgurls.contains(imgurl)) {
                                                    String imgurlFull = imgurl.startsWith("/") ? (hostFull + imgurl) : (url.substring(0, url.lastIndexOf("/") + 1) + imgurl);
                                                    contents = contents.replace(imgurl, imgurlFull);
                                                    imgurls.add(imgurl);
                                                }
                                            }
                                            article.setContents(contents);
                                            article.setContenttext(StringHelper.escapeHtml(contents));
                                        }
                                    }
                                    article.setPublishtime(new Date());
                                    article.setClicknum(0);
                                    article.setSort(0);
                                    factorySession.save(article);
                                }
                            }
                        }
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
